package helpers;

import static org.lwjgl.opengl.GL11.*;

import java.awt.Font;
import java.io.InputStream;
import java.io.Serializable;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.util.ResourceLoader;

import application.Window;

import interfaces.GraphicObject;

/**
 * Class for creating text.
 * 
 * @author Drahomir Karchnak, Tomas Zima
 */
public class Text extends GraphicObject implements Serializable
{
	/**
	 * Constructor.
	 * 
	 * @param text
	 */
	public Text(String text)
	{
		super();
	}
	
	/**
	 * Constructor.
	 * 
	 * @param x
	 * @param y
	 * @param text
	 */
	public Text(int x, int y, String text)
	{
		super(x, y);
		this.setText(text);
	}
	
	public Text(int x, int y, String text, Color color)
	{
		this(x, y, text);
		this.setColor(color);
	}
	
	/**
	 * Sets text.
	 * 
	 * @param text
	 */
	public void setText(String text)
	{
		this.text = text;
	}
	
	public void setColor(Color color)
	{
		this.color = color;
	}
	
	/**
	 * Redraw text.
	 */
	public void redraw()
	{
		boolean disableTexColMixing = true;
		if (Window.isTexColorMixingEnabled()) disableTexColMixing = false;
		
		Window.enableTexColorMixing();
		glDisable(GL11.GL_CULL_FACE);
		
		font.drawString(x, y, text, color);

		glEnable(GL11.GL_CULL_FACE);
		GL11.glFrontFace(GL11.GL_CW);	
	
		if (disableTexColMixing) Window.disableTexColorMixing();
	}
	
	/**
	 * Reloads font.
	 * 
	 * @param filename
	 * 	Filename of file with whole path.
	 */
	public static void reloadFont(String filename, int size)
	{
		try
		{
			InputStream inputStream	= ResourceLoader.getResourceAsStream("res/Fonts/" + filename + ".ttf");
			Font awtFont2			= Font.createFont(Font.TRUETYPE_FONT, inputStream);
			awtFont2				= awtFont2.deriveFont((float) size);
			font					= new TrueTypeFont(awtFont2, false);
		}	
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	// Static initialization
	// (default font)
	static
	{
		reloadFont("mini_pixel-7", 40);
	}
	
	private String	text;
	private Color	color = new Color(1.0f, 1.0f, 1.0f);
	
	private static TrueTypeFont font;
}
